package org.admin.entity.domain;


import com.baomidou.mybatisplus.annotation.*;
import lombok.AccessLevel;
import lombok.Data;
import lombok.Getter;
import lombok.Setter;

import java.util.Date;

@TableName("admin_entries")
@Data
public class Entry {
    @Getter
    public enum STATE {
        FAILURE(0),
        SUCCESS(1),
        LOGIN(2),
        LOGOUT(3);
        private final int value;

        STATE(int value) {
            this.value = value;
        }
    }

    public static final int FAILURE_COUNT = 3;
    public static final int FAILURE_TIME = 600 * 1000;
    public static final int TIME_OUT = 86400 * 1000;
    public static final int ALWAYS_TIME_OUT = 7 * 86400 * 1000;

    @TableId(value = "token", type= IdType.ASSIGN_UUID)
    private String token;

    @TableField("user_id")
    private Integer userId;

    @TableField("created_at")
    private Date createdAt;

    @TableField("updated_at")
    private Date updatedAt;

    @TableLogic
    @TableField("deleted_at")
    private Date deletedAt;

    @TableField("expired_at")
    private Date expiredAt;

    @TableField("failure_at")
    private Date failureAt;

    @Setter(AccessLevel.NONE)
    @TableField("failure_count")
    private Integer failureCount;

    @Setter(AccessLevel.NONE)
    private Integer state;

    @Setter(AccessLevel.NONE)
    @TableField(exist = false)
    private STATE stateWith;
    public void setFailureCount(Integer count){
        failureCount = Math.min(count, FAILURE_COUNT);
    }

    public void setState(Integer state){
        if(state != null) {
            for (STATE item : STATE.values()) {
                if (item.getValue() == state) {
                    stateWith = item;
                    break;
                }
            }
        }else{
            stateWith = null;
        }
        this.state = state;
    }

    public void setStateWith(STATE stateWith){
        if(stateWith != null){
            state = stateWith.getValue();
        }else{
            state = null;
        }
        this.stateWith = stateWith;
    }
}
